<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <%@include file="../../../../resources/inc/head.jsp" %>
</head>
<body>

<div class="layui-fluid">

    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto option-bar">

        </div>

        <div class="layui-card-body">
            <table id="applicationConfigManage" lay-filter="applicationConfigManage"></table>
            <script type="text/html" id="optionToolbar">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
                </div>
            </script>
            <script type="text/html" id="optionBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-warm layui-btn-xs"  lay-event="app">参数配置</a>
            </script>
            <script type="text/html" id="gmtModifiedTpl">
                {{#
                var date = new Date();
                date.setTime(d.gmtModified);
                return date.Format("yyyy-MM-dd hh:mm:ss");
                }}
            </script>

            <script type="text/html" id="scene">
                {{#
                return d.scene==null?'无':d.scene;
                }}
            </script>
            <script type="text/html" id="type">
                {{#
                return d.type==null?'无':d.type;
                }}
            </script>
            <script type="text/html" id="name">
                {{#
                return d.name==null?'无':d.name;
                }}
            </script>

            <script type="text/html" id="isDisabledTpl">
                <input type="checkbox" name="state" value="{{d.id}}" title="禁用" lay-filter="isDisabledFilter" {{
                       d.state== 1 ? 'checked' : '' }}>
            </script>
            <script type="text/html" id="avatar">
                <div><img src="{{d.avatar==null?'<%=basePath%>resources/images/noImage.jpg':d.avatar}}"
                          style="height: 28px" class="layui-table-link"></div>
            </script>
        </div>
    </div>
</div>

<%@include file="../../../../resources/inc/footer.jsp" %>
<script>
    layui.config({
        base: '<%=basePath%>resources/lib/layuiAdmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;
        var merchantNo = "${merchantNo}"
        table.render({
            elem: '#applicationConfigManage',
            url: '<%=basePath%>manager/merchant/applicationConfig/page/' + merchantNo + '.html',
            toolbar: '#optionToolbar',
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', width: 50, title: 'ID', sort: true}
                , {field: 'agentNo', title: '应用号'}
                , {field: 'name', title: '名称', templet: "#name"}
                , {field: 'scene', title: '场景', templet: "#phone"}
                , {field: 'type', title: '类型', templet: "#email"}
                , {title: '更新时间', sort: true, templet: "#gmtModifiedTpl"}
                , {title: "操作", width: 150, align: "center", fixed: "right", toolbar: "#optionBar"}

            ]],
            page: !0,
            limit: 10,
            height: "full-60",
            text: {none: "暂无可显示数据"},
            response: {
                statusCode: 200
            }
        });

        table.on('toolbar(applicationConfigManage)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    var diglog = new UtilClass.LayerDialog({
                        title: '添加应用'
                        , content: '<%=basePath%>manager/merchant/applicationConfig/create/'+merchantNo+'.html',
                    });
                    diglog.layerShow();
                    break;
                case 'dels':
                    var data = checkStatus.data;
                    var List = JSON.stringify(data);
                    var jsList = JSON.parse(List);
                    if (jsList[0] == null) {
                        layer.msg('请选中至少一行');
                    } else {
                        var chars = new Array();
                        for (var li in jsList) {
                            chars[li] = jsList[li].id;
                        }
                        var str = "";
                        for (var i = 0; i < chars.length; i++) {
                            if (i == chars.length - 1) {
                                str += chars[i];
                            } else {
                                str = str + chars[i] + ",";
                            }

                        }
                        layer.load(2);
                        $.ajax({
                            type: "get",
                            url: "<%=basePath%>manager/permissionUser/delete/" + str + ".html",
                            success: function (data) {
                                layer.closeAll("loading");
                                data = JSON.parse(data);


                                if (data.success) {
                                    layer.msg("删除成功");
                                    table.reload('applicationConfigManage');

                                } else {
                                    layer.msg(data.message, {icon: 5});
                                }
                            }
                        });
                        break;
                    }
            }
            ;
        });
        table.on("tool(applicationConfigManage)", function (e) {
            console.log(e.data);
            if ("del" === e.event) {
                layer.confirm("真的删除行么", function (t) {
                    layer.close(t);

                    var List = JSON.stringify(e.data);
                    var jsList = JSON.parse(List);
                    var ids = jsList.id;

                    layer.load(2);
                    $.ajax({
                        type: "get",
                        url: "<%=basePath%>manager/permissionUser/delete/" + ids + ".html",
                        success: function (data) {
                            layer.closeAll("loading");
                            data = JSON.parse(data);

                            if (data.success) {
                                table.reload('applicationConfigManage');

                            } else {
                                layer.msg(data.message, {icon: 5});
                            }
                        }
                    });

                })
            } else if ("edit" === e.event) {
                var diglog = new UtilClass.LayerDialog({
                    title: "编辑[" + e.data.name + "]"
                    , content: "<%=basePath%>manager/merchant/applicationConfig/update/" + e.data.id + ".html",
                });
                diglog.layerShow();
            }else if ("app" === e.event) {
                var diglog = new UtilClass.LayerDialog({
                    title: "配置[" + e.data.name + "]"
                    , content: "<%=basePath%>manager/merchant/applicationConfig/config.html?id="+e.data.id,
                });
                diglog.layerShow();
            }
            /*else if("userRole"===e.event){
                var data = e.data;
                var id = data.id;
                layer.open({
                    type: 2
                    , title: '分配角色(' + data.userName + ')'
                    , content: '
            <%=basePath%>manager/permissionUser/addRole/' + id + '.html'
                    , area: ['450px', '80%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index];
                        var submitID = 'LAY-submit'
                        var submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var formData = data.field;
                            var roleIds = "" + formData["roleIds"] + "";
                            $.ajax({
                                type: "POST",
                                url: "
            <%=basePath%>manager/permission/userRole/allocationUserRole.action",
                                data: {userId: id, roleIds: roleIds},
                                async: false,
                                success: function (data) {
                                    data = JSON.parse(data);
                                    if (data.success) {
                                        layer.msg(data.message);
                                        layer.close(index);
                                    } else {
                                        layer.msg(data.message);
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            }*/
        });

        //监听搜索
        form.on('submit(Type-serch)', function (data) {
            var field = data.field;
            $(".layui-laypage-skip").find("input").val("1");
            $(".layui-laypage-btn").click();
            table.reload('applicationConfigManage', {
                where: {userName: field.userName}
            });
        });


    });

    function edit(e) {

    }

</script>
</body>
</html>

